<?php
if(!defined('IN_IBEIKE')) {
	exit('Access Denied');
}

class table_city_photo extends common_table
{
	public function __construct() {

		$this->_table = 'city_photo';
		$this->_pk    = 'tid'; 

		parent::__construct();
	}
	
	public function count_by_tid($tid) {
		return DB::result_first('SELECT COUNT(*) FROM %t WHERE tid = %d', array($this->_table, $tid));
	}
	
	public function count_by_authorid($authorid) {
		return DB::result_first('SELECT COUNT(*) FROM %t WHERE authorid = %d', array($this->_table, $authorid));
	}
	
	public function delete_by_authorid($authorid) {
		if(!$authorid) {
			return null;
		}
		return DB::delete($this->_table, DB::field('authorid', $authorid));
	}
	
	public function delete_by_tid($tid) {
		if(!$tid) {
			return null;
		}
		return DB::delete($this->_table, DB::field('tid', $tid));
	}
	
	public function update_by_tid($tid, $data) {
		if(!is_array($data) || empty($data)) {
			return null;
		}
		return DB::update($this->_table, $data, DB::field('tid', $tid));
	}
	
	public function update_num_by_tid($num, $tid, $numlimit = false) {
		$args = array($this->_table, $num, $tid);
		if($numlimit !== false) {
			$sql = ' AND num>0';
			$args[] = $numlimit;
		}
		return DB::query("UPDATE %t SET num=num+'%d' WHERE tid=%d {$sql}", $args);
	}
	
	public function update_num_by_authorid($num, $authorid, $numlimit = false) {
		$args = array($this->_table, $num, $authorid);
		if($numlimit !== false) {
			$sql = ' AND num>0';
			$args[] = $numlimit;
		}
		return DB::query("UPDATE %t SET num=num+'%d' WHERE authorid=%d {$sql}", $args);
	}
	
	public function fetch_tid_by_author($authors) {
		if(!$authors) {
			return null;
		}
		return DB::fetch_all('SELECT tid FROM %t WHERE author IN (%n)', array($this->_table, $authors), 'tid');
	}
	
	
}
